<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Charge_list_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function search_chg_list($name){
       $sql = "SELECT * FROM ubs_chg_list WHERE list_name LIKE  '".$name."' ";
        $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }
    public function insert_chg_list($data){
        $this->db->query("call ubs_chg_list_ins(?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function update_chg_list($data){
        $this->db->query("call ubs_chg_list_upd(?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function insert_chg_list_line($data){
        $this->db->query("call ubs_chg_list_line_ins(?,?,?,?,?,@id,@err,@msg)",$data);
        $que = $this->db->query("SELECT @id as id , @err as error , @msg as msg");
        return $que->result();
    }
    public function delete_chg_list_line($data){
        $this->db->query("call ubs_chg_list_line_del(?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        return $que->result();
    }
    public function update_seq($data){
        $this->db->query("call ubs_chg_list_line_upd(?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }
    public function get_sequence($id){
        $sql="SELECT ucll.line_id line_id, ucll.list_id list_id, ucll.seq_num seq_num, uce.charge_name charge_name, ucep.option_name option_name , ucep.option_value option_value FROM ubs_chg_list_line ucll INNER JOIN ubs_chg_elements uce ON ucll.charge_code = uce.charge_code INNER JOIN ubs_chg_element_option ucep ON ucll.option_code = ucep.option_code
          WHERE ucll.list_id = '".$id."' ";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function get_chg_code($id){
        $this->db->where('charge_id', $id);
        $query = $this->db->get('ubs_chg_elements');
        return $query->result();
    }
    public function get_charges(){
      $sql = "SELECT * FROM ubs_chg_elements";
      $query = $this->db->query($sql);
      return $query->result();
    }
    public function get_chg_list($id){
        $this->db->where('list_id', $id);
        $query = $this->db->get('ubs_chg_list');
        return $query->result();
    }
    public function get_options($id){
        $this->db->where('charge_id', $id);
        $query = $this->db->get('ubs_chg_element_option');
        return $query->result();
    }
    public function get_option_value($code){
        $this->db->where('option_code', $code);
        $query = $this->db->get('ubs_chg_element_option');
        return $query->result();
    }
    
}